package se.lnu.axez;

import java.util.Map;

import android.opengl.GLES20;
import android.util.Log;

public final class Utils {
	public static final boolean ReportGlErrors = true;
	
	private Utils() {}
	
    public static void checkGlError(String tag, String operation) {
    	if (!ReportGlErrors) {
    		return;
    	}
    	
        int error;
        while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
        	String message = String.format("%s failed. GL Error: %s", operation, error);
            Log.e(tag, message);
            throw new RuntimeException(message);
        }
    }
    
    public static <T, E extends T> E getOrCreate(Class<E> type, Map<Class<? extends T>, T> map) {
		if (map.containsKey(type)) {
			return (E) map.get(type);
		}

		try {
			E instance = type.newInstance();
			map.put(type, instance);
			return instance;
		} catch (IllegalAccessException e) {
			throw new RuntimeException();
		} catch (InstantiationException e) {
			throw new RuntimeException();
		}

    }
}
